package com.hb.config.shiro;

import com.hb.demo.entity.AdminUser;
import com.hb.util.JwtUtils;
import lombok.Getter;
import lombok.Setter;
import org.apache.shiro.authc.AuthenticationToken;

/**
 * @Author Scott
 * @create 2018-07-12 15:19
 * @desc
 **/
@Setter
@Getter
public class JwtToken implements AuthenticationToken {

    /**
     * 用户ID
     */
    private Long id;
    /**
     * jwt token验证字符串
     */
    private String token;
    /**
     * 用户名
     */
    private String username;
    /**
     * 密码(加密后的)
     */
    private String password;
    /**
     * 用户姓名
     */
    private String fullName;

    public JwtToken() {

    }

    public JwtToken(String token) {
        this.token = token;
        this.id = JwtUtils.getUserId(token);
        this.username = JwtUtils.getUsername(token);
        this.password = JwtUtils.get(token, "password", String.class);
    }

    public JwtToken(String username, String password) {
        this(null, username, password);
    }

    public JwtToken(String token, String username, String password) {
        this(token);
        this.username = username;
        this.password = password;
    }

    @Override
    public Object getPrincipal() {
        return token;
    }

    @Override
    public Object getCredentials() {
        return password;
    }

    public AdminUser getUserEntity() {
        AdminUser user = new AdminUser();
        user.setId(id);
        user.setUname(username);
        user.setRealName(fullName);
        return user;
    }
}
